#include "ccalapp.h"

#include <buola/algorithm/container.h>
#include <buola/dbus/cproxy.h>

namespace buola { namespace bcal {

CCmdLineFlag CCalApp::mOptionService("service",
            L"launches as service, which doesn't open any window");

CCalApp::CCalApp()
    :   dbus::CObject<CCalApp>("/org/buola/BCal")
{
    mWindow=nullptr;
}

CCalApp::~CCalApp()
{
}

bool CCalApp::Activate()
{
    if(dbus::get_session_bus()->RegisterService("org.buola.BCal")) return true;

    //if we can't register... recycle existing window
    if(!cmd_line().IsSet(mOptionService))
    {
        dbus::CProxy lProxy(dbus::get_session_bus(),"org.buola.BCal","/org/buola/BCal");
        lProxy.CallIgnore("org.buola.BCal","RaiseWindow");
    }

    return false;
}

bool CCalApp::Start()
{
    if(!Activate()) return false;
    CApp::Start();

    mManager=new CCalManager;

    if(!cmd_line().IsSet(mOptionService))
    {
        mWindow=new CCalWindow(mManager);
        mWindow->Map(CSize_d(900,900));
    }

    for(int i=0;i<cmd_line().ArgCount();i++)
    {
        mManager->OpenFileCalendar(io::file(cmd_line().Arg(i)));
    }

    return true;
}

/*namespace buola*/ } /*namespace bcal*/ }
